<template>
  <div class="page-header-index-wide">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <!-- 搜索区域 -->
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="10" :sm="12">
            <a-form-item label="认筹时间" :labelCol="{span: 5}">
              <j-date v-model="queryParam.timestampBegin"
                      date-format="YYYY-MM-DD"
                      style="width:45%" placeholder="请选择开始时间" >
              </j-date>
              <span style="width: 10px;">~</span>
              <j-date v-model="queryParam.timestampEnd"
                      date-format="YYYY-MM-DD"
                      style="width:45%" placeholder="请选择结束时间" >
              </j-date>
            </a-form-item>
          </a-col>
          <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
            <a-col :md="6" :sm="24">
              <a-button type="primary" @click="searchQuery">查询</a-button>
              <a-button style="margin-left: 8px" @click="searchReset">重置</a-button>
            </a-col>
          </span>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator"  style="margin: 5px 0 18px 0">
      <a-button @click="batchSettle" type="primary" icon="plus">批量结算</a-button>
    </div>

    <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
      <i class="anticon anticon-info-circle ant-alert-icon"></i>已选择&nbsp;<a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项&nbsp;&nbsp;
      <a style="margin-left: 24px" @click="onClearSelected">清空</a>
    </div>
    <a-table
      ref="table"
      bordered
      size="middle"
      rowKey="index"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      :loading="loading"
      :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
      @change="handleTableChange">

      <span slot="action" slot-scope="text, record">
        <a @click="settle(record)">结算</a>
      </span>
    </a-table>
  </div>
</template>

<script>
  import "../../utils/tron/tronweb.js"


  import {TronAPI_Token} from "../../utils/tron/api-tron-token.js"
  import {ContractTokenConfig} from "../../utils/tron/contract-token-config"

  import {JeecgListMixin} from '@/mixins/JeecgListMixin'
  import JDate from '@/components/jeecg/JDate'
  import {betHandle} from "@/api/api"
  export default {
    name: "BetSettle",
    mixins: [JeecgListMixin],
    components: {
      JDate,
    },
    data() {
      return {
        columns: [
          {
            title: '账户',
            align: "center",
            dataIndex: 'caller',
          },
          {
            title: '认筹金额',
            align: "center",
            dataIndex: 'amount',
          },
          {
             title: '认筹时间',
             align: "center",
             width: 150,
             dataIndex: 'date',
           },
          {
            title: '操作',
            dataIndex: 'action',
            scopedSlots: {customRender: 'action'},
            align: "center",
            width: 150,
          }
        ],
        isorter:{
          column: 'timestamp',
          order: 'desc',
        },
        url:{
          list: "/bet/isSettle/list"
        }
      }
    },
    created(){
      this.settleListen()
    },
    methods: {
      async settleListen(){
        //监听投注事件
        let that = this
        let tokenContract = await window.tronWeb.contract().at(ContractTokenConfig.contractAddress)
        tokenContract["Settle"]().watch((err, result) => {
          that.$notification.success({
            message: '审核通过'
          });
        })
      },
      batchSettle(){
        let that = this
        this.selectedRowKeys.forEach(function(value){
          that.settle(that.dataSource[value])
        })
      },
      settle(record){
        let date = parseInt(record.date.replace(/-/g, ""));
        let address = record.caller
        TronAPI_Token.settle(date, address)
          .then(trxId => {
            console.log(trxId)
          })
      },
    }
  }
</script>

<style lang="scss" scoped>
  .page-header-index-wide{
    padding: 24px 32px;
    background-color: #fff;
  }
</style>